Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

267
Views
Django problem with missing " underscore" in url

I am a beginner and need a little support. Everything works in the application, but when in the form it typing "new york", "_" is missing in the url.

My code is

def index(request):
    if request.method == 'POST':
        city = request.POST['city']
   
        if city == '':
            return redirect('index')
        res = urllib.request.urlopen(f'http://api.openweathermap.org/data/2.5/weather?q={city}&appid=1b79ea7b1251b76ccda81d6bd').read()
       

        json_data = json.loads(res)
        datas = {
            "country_code" : str(json_data['sys']['country']),
            "cordinate": str(json_data['coord']['lon'])+ ''+ 
            str(json_data['coord']['lat']),
            "temp": str(int(json_data['main']['temp']-273)) + 'C',
            "pressure" : str(json_data['main']['pressure']),
            "humidity" : str(json_data['main']['humidity']),

        }


In response receives "

InvalidURL at /
URL can't contain control characters. '/data/2.5/weather?q=bielsko biala&appid=1b79ea7b1251b76cc0b41a81d6bd' (found at least ' ')

I am looking for solutions on the Internet but I cannot solve it myself. Thank you very much for all your help

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You just need to replace spaces in city with the encoded values.

Try this:

import urllib.parse    
res = urllib.request.urlopen(f'http://api.openweathermap.org/data/2.5/weather?q={urllib.parse.quote(city)}&appid=1b79ea7b1251b76ccda81d6bd').read()

this is to encode city in the URL.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!